package com.rzgeo.rzmaplib.comp;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.esri.arcgisruntime.mapping.view.MapView;
import com.rzgeo.rzmaplib.R;

/**
 * Created by zhanghang on 2018/1/22.
 */

public class RmlMapToolKit extends FrameLayout {
    private View btnZoomIn;
    private View btnZoomOut;
    private View btnZoomHomeView;
    private View btnToNorth;
    private View btnLocate;
    private MapView _mapView;

    public RmlMapToolKit(@NonNull Context context) {
        super(context);
        initUI(context);
    }

    public RmlMapToolKit(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initUI(context);
    }

    public RmlMapToolKit(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initUI(context);
    }

    private void initUI(Context context) {
        View subView = LayoutInflater.from(context).inflate(R.layout.comp_map_toolkit, null);
        subView.setLayoutParams(
                new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
        this.addView(subView);
    }

    public void setup(MapView mapView){
        _mapView=mapView;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        btnZoomIn=findViewById(R.id.btn_zoom_in);
        btnZoomOut=findViewById(R.id.btn_zoom_out);
        btnZoomHomeView=findViewById(R.id.btn_zoom_homeview);
        btnToNorth=findViewById(R.id.btn_to_north);
        btnLocate=findViewById(R.id.btn_locate);

        btnZoomIn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(_mapView==null)
                    return;
                double scalse = _mapView.getMapScale() / 2;
                _mapView.setViewpointScaleAsync( scalse);
            }
        });
        btnZoomOut.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(_mapView==null)
                    return;
                double scalse = _mapView.getMapScale() * 2;
                _mapView.setViewpointScaleAsync( scalse);
            }
        });
        btnZoomHomeView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        btnToNorth.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(_mapView==null)
                    return;
                _mapView.setRotation(0);
            }
        });
        btnLocate.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(_mapView==null)
                    return;
                if(_mapView.getLocationDisplay().isStarted()){
                    _mapView.getLocationDisplay().stop();
                }else{
                    _mapView.getLocationDisplay().startAsync();
                }
            }
        });

    }
}
